﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeGenerator.Core;

namespace CodeGenerator.UI
{
    public partial class FrmSelectTable : Form
    {
        public FrmSelectTable()
        {
            InitializeComponent();
        }

        #region Field
        /// <summary>
        /// 返回选择的表
        /// </summary>
        public List<string> selectTable;
        #endregion
        
        #region Action
        private void FrmSelectTable_Load(object sender, EventArgs e)
        {
            ServerInfo server=Constant.CurrentServerInfo;

            if (server != null && !string.IsNullOrEmpty(Constant.DatabaseName))
            {

                IDatabase database = DBFactory.Database(server.DBType);
                var tables = database.GetTableList(server, Constant.DatabaseName);
                foreach (var table in tables)
                {
                    lst_Info.Items.Add(table.Name);
                }
            }
            else
            {
                Common.MessageUtil.ShowWarning("请先选择一个数据库！！！");
                this.Close();
            }
        }

        private void btn_RightMoveAll_Click(object sender, EventArgs e)
        {
            lst_UseInfo.Items.AddRange(lst_Info.Items);
            lst_Info.Items.Clear();
        }

        private void btn_LeftMoveAll_Click(object sender, EventArgs e)
        {
            lst_Info.Items.AddRange(lst_UseInfo.Items);
            lst_UseInfo.Items.Clear();
        }

        private void btn_RightMove_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lst_Info.Items.Count; i++)
            {
                if (lst_Info.GetSelected(i))
                {
                    lst_UseInfo.Items.Add(lst_Info.Items[i]);
                    lst_Info.Items.RemoveAt(i);
                }
            }
        }

        private void btn_LeftMove_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lst_UseInfo.Items.Count; i++)
            {
                if (lst_UseInfo.GetSelected(i))
                {
                    lst_Info.Items.Add(lst_UseInfo.Items[i]);
                    lst_UseInfo.Items.RemoveAt(i);
                }
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            if (lst_UseInfo.Items.Count > 0)
            {
                selectTable = new List<string>();
                for (int i = 0; i < lst_UseInfo.Items.Count; i++)
                {
                   // CodeGenerator.Core.Entity.CodeCreate entity = new Core.Entity.CodeCreate();
                   // entity.ConfigNameSpace=

                    selectTable.Add(lst_UseInfo.Items[i].ToString());
                } 
            }
            this.Close();
        }

        private void lst_Info_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btn_RightMove_Click(sender,e);
        }

        private void lst_UseInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            btn_LeftMove_Click(sender, e);
        }
        #endregion
    }
}
